package yg.engine;




import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.SurfaceView;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;

public class YGActivity extends Activity 
{

	@Override 
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
				
		// package net.fhtagn.moob
		
		String apkFilePath = null;
		ApplicationInfo appInfo = null;
		PackageManager packMgmr = getPackageManager();
		try 
		{
			appInfo = packMgmr.getApplicationInfo("yg.game.Galaxians", 0);
		} 
		catch (NameNotFoundException e) 
		{
			e.printStackTrace();
			throw new RuntimeException("Unable to locate assets, aborting...");
		}
		apkFilePath = appInfo.sourceDir;
		
		NativeSetup(apkFilePath);
		//NativeSetup("");
		
		// fullscreen mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags (
			WindowManager.LayoutParams.FLAG_FULLSCREEN,
			WindowManager.LayoutParams.FLAG_FULLSCREEN
		); 
		
		m_View = new YGView(this);
		//m_View.setText("Initializing");
		setContentView(m_View);
	}

	@Override 
	protected void onPause() 
	{
		super.onPause();
		
		System.out.println("YGActivity: onPause");
		
		/*if (NativeExit())
		{
			System.out.println("YGView: Exiting");
			this.finish();
		}*/
		//m_View.onPause();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (event.getAction() == KeyEvent.ACTION_DOWN)
		{
			NativeKeyPress(keyCode);
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		if (event.getAction() == KeyEvent.ACTION_UP)
		{
			NativeKeyRelease(keyCode);
			return true;
		}
		
		return super.onKeyUp(keyCode, event);
	}
	
	public void onConfigurationChanged(Configuration newConfig)
	{
		super.onConfigurationChanged(newConfig);
		
		m_View = new YGView(this);
		setContentView(m_View);
		
		/*m_View.getRenderer().NativeResize(
			getWindowManager().getDefaultDisplay().getWidth(),
			getWindowManager().getDefaultDisplay().getHeight()
		);*/
		
		System.out.println("YGActivity: onConfigurationChanged");	
	}
	
	public void CloseApp()
	{
		System.out.println("YGActivity: CloseApp");
		this.finish();
	}
	

	@Override 
	protected void onResume() 
	{
		super.onResume();
		
		System.out.println("YGActivity: onResume");
		//m_View.onResume();
	}
	
	@Override 
	protected void onDestroy()
	{
		super.onDestroy();
		
		System.out.println("YGActivity: onDestroy");
		
		NativeDone();
	}

	protected YGView m_View;
	
	protected native void NativeSetup(String a_PackagePath);
	protected native void NativeKeyPress(int a_KeyCode);
	protected native void NativeKeyRelease(int a_KeyCode);
	protected native void NativeDone();
	
}